package com.atguigu.service.impl;

import com.atguigu.entity.ProductImage;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.ProductSalePropertyValue;
import com.atguigu.entity.ProductSpu;
import com.atguigu.mapper.ProductSpuMapper;
import com.atguigu.service.ProductImageService;
import com.atguigu.service.ProductSalePropertyKeyService;
import com.atguigu.service.ProductSalePropertyValueService;
import com.atguigu.service.ProductSpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author hachimen
 * @since 2021-08-24
 */
@Service
public class ProductSpuServiceImpl extends ServiceImpl<ProductSpuMapper, ProductSpu> implements ProductSpuService {

    @Autowired
    private ProductSalePropertyKeyService productSalePropertyKeyService;
    @Autowired
    private ProductSalePropertyValueService productSalePropertyValueService;
    @Autowired
    private ProductImageService productImageService;

    @Override
    public void saveProductSpu(ProductSpu productSpu) {
        baseMapper.insert(productSpu);
        List<ProductImage> productImageList = productSpu.getProductImageList();//图片
        List<ProductSalePropertyKey> salePropertyKeyList = productSpu.getSalePropertyKeyList();//key
        if(!CollectionUtils.isEmpty(productImageList)){//补全图片属性
            for (ProductImage productImage : productImageList) {
                productImage.setProductId(productSpu.getId());
            }
            productImageService.saveBatch(productImageList);
        }
        if(!CollectionUtils.isEmpty(salePropertyKeyList)){//补全key属性
            List<ProductSalePropertyValue> salePropertyValueListBeach = new ArrayList<>();
            for (ProductSalePropertyKey productSalePropertyKey : salePropertyKeyList) {
                productSalePropertyKey.setProductId(productSpu.getId());
                List<ProductSalePropertyValue> salePropertyValueList = productSalePropertyKey.getSalePropertyValueList();//value
                if(!CollectionUtils.isEmpty(salePropertyValueList)){//补全value属性
                    for (ProductSalePropertyValue productSalePropertyValue : salePropertyValueList) {
                        productSalePropertyValue.setProductId(productSpu.getId());
                        productSalePropertyValue.setSalePropertyKeyName(productSalePropertyKey.getSalePropertyKeyName());
                    }
                    //productSalePropertyValueService.saveBatch(salePropertyValueList);
                    salePropertyValueListBeach.addAll(salePropertyValueList);//循环并入批处理用List中
                }
            }
            productSalePropertyKeyService.saveBatch(salePropertyKeyList);
            productSalePropertyValueService.saveBatch(salePropertyValueListBeach);//提交批处理list，避免循环提交
        }
    }
}
